package com.ede.roverhu.simplebackstage.Ibeacon;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.util.Base64;

import java.util.UUID;

/**
 * Created by Admin on 2017/6/29.
 */

public class BLEIbeaconEdit {
    /**
     * 获取service uuid
     *
     * @param service
     * @param mBluetoothGatt
     */
    public static void readServiceUUID(BluetoothGattService service, BluetoothGatt mBluetoothGatt) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff0-0000-1000-8000-00805f9b34fb"));
        mBluetoothGatt.readCharacteristic(characteristic);
//        mBluetoothGatt.setCharacteristicNotification(characteristic, true);
    }

    /**
     * 读取 UUID
     *
     * @param service
     * @param mBluetoothGatt
     */
    public static void readUUID(BluetoothGattService service, BluetoothGatt mBluetoothGatt) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff1-0000-1000-8000-00805f9b34fb"));
        mBluetoothGatt.readCharacteristic(characteristic);
//        mBluetoothGatt.setCharacteristicNotification(characteristic, true);
    }

    /**
     * 写入UUID
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void writeUUID(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff1-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }


    /**
     * 读取Major
     *
     * @param service
     * @param mBluetoothGatt
     */
    public static boolean readMajor(BluetoothGattService service, BluetoothGatt mBluetoothGatt) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff2-0000-1000-8000-00805f9b34fb"));
        boolean b = mBluetoothGatt.readCharacteristic(characteristic);
        return b;
    }

    /**
     * 写入Major
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void writeMajor(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff2-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

    /**
     * 读取Minor
     *
     * @param service
     * @param mBluetoothGatt
     */
    public static void readMinor(BluetoothGattService service, BluetoothGatt mBluetoothGatt) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff3-0000-1000-8000-00805f9b34fb"));
        mBluetoothGatt.readCharacteristic(characteristic);
    }

    /**
     * 写入Minor
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void writeMinor(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff3-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

    /**
     * 读取MeasuredPower
     *
     * @param service
     * @param mBluetoothGatt
     */
    public static void readMeasuredPower(BluetoothGattService service, BluetoothGatt mBluetoothGatt) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff4-0000-1000-8000-00805f9b34fb"));
        mBluetoothGatt.readCharacteristic(characteristic);
    }

    /**
     * 写入MeasuredPower
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void writeMeasuredPower(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff4-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

    /**
     * 读取TransmissionPower
     *
     * @param service
     * @param mBluetoothGatt
     */
    public static void readTransmissionPower(BluetoothGattService service, BluetoothGatt mBluetoothGatt) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff5-0000-1000-8000-00805f9b34fb"));
        mBluetoothGatt.readCharacteristic(characteristic);
    }

    /**
     * 写入TransmissionPower
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void writeTransmissionPower(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff5-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

    /**
     * 修改密码
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void writeChangePwd(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff6-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

    /**
     * 写入BroadcastingCycle
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void writeBroadcastingCycle(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff7-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

    /**
     * 读取BroadcastingCycle
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void readBroadcastingCycle(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff7-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

    /**
     * 写入SerialNum
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void writeSerialNum(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff8-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

    /**
     * 读取BroadcastingCycle
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void readSerialNum(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff8-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

    /**
     * 写入IbeaconName
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void writeIbeaconName(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff9-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

    /**
     * 读取IbeaconName
     *
     * @param service
     * @param mBluetoothGatt
     * @param str
     */
    public static void readIbeaconName(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String str) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fff9-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue(str.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }


    /**
     * 写入Authentication
     *
     * @param service
     * @param mBluetoothGatt
     */
    public static boolean wirteAuthentication(BluetoothGattService service, BluetoothGatt mBluetoothGatt) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fffa-0000-1000-8000-00805f9b34fb"));
        characteristic.setValue("AcCrEdItiSOK".getBytes());
        boolean b = mBluetoothGatt.writeCharacteristic(characteristic);
        return b;
    }

    /**
     * 读取Authentication
     *
     * @param service
     * @param mBluetoothGatt
     */
    public static boolean readAuthentication(BluetoothGattService service, BluetoothGatt mBluetoothGatt) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fffa-0000-1000-8000-00805f9b34fb"));
        boolean b = mBluetoothGatt.readCharacteristic(characteristic);
        return b;

    }

    /**
     * 写入ConnectionMode
     *
     * @param service
     * @param mBluetoothGatt
     * @param Mode
     */

    public static void wirteConnectionMode(BluetoothGattService service, BluetoothGatt mBluetoothGatt, String Mode) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fffe-0000-1000-8000-00805f9b34fb"));
        if (!"0".equals(Mode) && !"1".equals(Mode)) {
            Mode = "0";
        }
        characteristic.setValue(Mode.getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

    /**
     * 读取ConnectionMode
     *
     * @param service
     * @param mBluetoothGatt
     */

    public static void readConnectionMode(BluetoothGattService service, BluetoothGatt mBluetoothGatt) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000fffe-0000-1000-8000-00805f9b34fb"));
        mBluetoothGatt.readCharacteristic(characteristic);
    }


    /**
     * Reboot BLE
     *
     * @param service
     * @param mBluetoothGatt
     */
    public static void RebootBLE(BluetoothGattService service, BluetoothGatt mBluetoothGatt) {
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(
                "0000ffff-0000-1000-8000-00805f9b34fb"));
        //我这个IBeacon设备的密码是“minew123”,通过UnicodeToBase64转换后是"bWluZXcxMjM"
        characteristic.setValue(Base64.decode("AA1406111".getBytes(), Base64.DEFAULT));
        mBluetoothGatt.writeCharacteristic(characteristic);
    }


}
